package com.tan.lbs.dao.impl;

import java.util.ArrayList;
import java.util.List;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.criterion.Restrictions;

import com.tan.lbs.dao.GameTriggerDAO;
import com.tan.lbs.entity.GameCondition;
import com.tan.lbs.entity.GameTrigger;
import com.tan.lbs.entity.Item;
import com.tan.lbs.util.HibernateUtil;

public class GameTriggerDAOImpl implements GameTriggerDAO {

	@Override
	public String create(GameTrigger gameTrigger) {
		SessionFactory sf = HibernateUtil.getSessionFactory();
		Session s = sf.getCurrentSession();
		s.beginTransaction();
		s.save(gameTrigger);
		s.getTransaction().commit();
		return gameTrigger.getTriggerID();
	}

	@Override
	public GameTrigger readByID(String triggerID) {
		SessionFactory sf = HibernateUtil.getSessionFactory();
		Session s = sf.getCurrentSession();
		s.beginTransaction();
		GameTrigger gameTrigger = (GameTrigger)s.get(GameTrigger.class, triggerID);
		s.getTransaction().commit();
		return gameTrigger;
	}

	@Override
	public void update(GameTrigger gameTrigger) {
		SessionFactory sf = HibernateUtil.getSessionFactory();
		Session s = sf.getCurrentSession();
		s.beginTransaction();
		s.update(gameTrigger);
		s.getTransaction().commit();
	}

	@Override
	public void delete(String triggerID) {
		SessionFactory sf = HibernateUtil.getSessionFactory();
		Session s = sf.getCurrentSession();
		s.beginTransaction();
		GameTrigger gameTrigger = new GameTrigger();
		gameTrigger.setTriggerID(triggerID);
		s.delete(gameTrigger);
		s.getTransaction().commit();
	}

	@Override
	public GameTrigger readByObjectAndTypeAndGame(String actionObjectID,
			String actionType,String gameID) {
		SessionFactory sf = HibernateUtil.getSessionFactory();
		Session s = sf.getCurrentSession();
		s.beginTransaction();
		List<GameTrigger> gs = s.createCriteria(GameTrigger.class).add(Restrictions.
				eq("actionObjectID", actionObjectID)).add(Restrictions.
				eq("actionType", actionType)).add(Restrictions.
						eq("gameID", gameID)).list();
		s.getTransaction().commit();
		if(gs.size() > 0) return gs.get(0);
		else return null;
	}
	
	@Override
	public void deleteByGameObject(String gameObjectID) {
		ArrayList<GameTrigger> triggers = searchByGameObject(gameObjectID);
		SessionFactory sf = HibernateUtil.getSessionFactory();
		Session s = sf.getCurrentSession();
		s.beginTransaction();
		for(GameTrigger trigger : triggers) {
			GameTrigger deleteTrigger = new GameTrigger();
			deleteTrigger.setTriggerID(trigger.getTriggerID());
			s.delete(deleteTrigger);
		}
		s.getTransaction().commit();
		
	}

	@Override
	public ArrayList<GameTrigger> searchByGameObject(String gameObjectID) {
		SessionFactory sf = HibernateUtil.getSessionFactory();
		Session s = sf.getCurrentSession();
		s.beginTransaction();
		List<GameTrigger> gs = s.createCriteria(GameTrigger.class).add(Restrictions.
				eq("actionObjectID", gameObjectID)).list();
		s.getTransaction().commit();
		return new ArrayList<GameTrigger>(gs);
	}
	
	

}
